package gao.xaiolei.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import gao.xaiolei.dto.*;
import gao.xaiolei.enity.*;
import gao.xaiolei.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class TaskController {

    @Resource(name = "TaskService")
    private TaskService taskService;

    // 新建任务等价于@PostMapping（{}),但在gateWay下如果前面没有@RequestMapping用""会报404
    @PostMapping
    public void addTask(@RequestBody TaskInDto taskInDto) {
        taskService.addTask(taskInDto);
    }

    // 修改任务@PutMapping（{}),但在gateWay下如果前面没有@RequestMapping用""会报404
    @PutMapping
    public void updateTask(@RequestBody TaskInDto taskInDto) {
        taskService.updateTask(taskInDto);
    }

    @PutMapping(value = "/status/responsible")
    public void updateTaskStatusForResponsible(
            @RequestBody List<String> taskIds) {
        taskService.updateTaskStatusForResponsible(taskIds);
    }

    @PutMapping(value = "/status/receiver/{receiverId}")
    public void updateTaskStatusForReceiver(
            @RequestBody List<String> taskIds,
            @PathVariable("receiverId")String receiverId) {
        taskService.updateTaskStatusForReceiver(taskIds,receiverId);
    }

    @GetMapping(value = "/taskListByReceiver/{id}/{status}")
    public TaskReceiverListDto getTaskListByReceive(@PathVariable("id") String receiverId,
                                                    @PathVariable(name = "status", required = false) Integer status,
                                                    @RequestParam(name = "page") Integer page,
                                                    @RequestParam(name = "limit") Integer size) {
        return taskService.getTaskListByReceiverId(receiverId, status,page,size);
    }

    @GetMapping(value = "/taskListByResponsible/{id}/{status}")
    public TaskResponseListDto getTaskListByResponsible(@PathVariable("id") String responsible,
                                                        @PathVariable(name = "status", required = false) Integer status,
                                                        @RequestParam(name = "page") Integer page,
                                                        @RequestParam(name = "limit") Integer size) {
        return taskService.getTaskListByResponsible(responsible, status,page,size);
    }

    @GetMapping(value = "/taskDetailForReceiver/{id}/{currentUserId}")
    public TaskOutDto getTaskByIdForReceiver(@PathVariable("id") String id,
                                             @PathVariable("currentUserId") String currentUserId) {
        return taskService.getTaskByIdForReceiver(id, currentUserId);
    }

    @GetMapping(value = "/taskDetailForResponsible/{id}")
    public TaskOutDto getTaskByIdForResponsible(@PathVariable("id") String id) {
        return taskService.getTaskByIdForResponsible(id);
    }

    @PostMapping(value = "/uploadAttached")
    public String uploadAttached(
            @RequestParam(value = "fileType", required = true) int fileType,
            @RequestParam(value = "file", required = true) MultipartFile file)
            throws IllegalStateException, IOException {
        return taskService.uploadAttached(fileType, file);
    }


    @GetMapping(value = "/attached/{fileType}/{hashOrUuid}")
    public byte[] getAttached(
            @PathVariable("hashOrUuid") String hashOrUuid,
            @PathVariable("fileType") int fileType) {
        return taskService.getAttached(hashOrUuid, fileType);
    }

//	@DeleteMapping(value = "/attached/{fileType}/{hashOrUuid}/{attachedId}")
//	public void deleteAttached(
//			@PathVariable("hashOrUuid") String hashOrUuid,
//			@PathVariable(value = "fileType") int fileType,
//			@PathVariable(value = "attachedId") String attachedId) {
//		taskService.deleteAttached(hashOrUuid, fileType, attachedId);
//	}

//	@PostMapping(value = "/attached")
//	public void addAttached(@RequestBody TaskAttached attached){
//		taskService.addAttached(attached);
//	}


//	// 保存单个链接信息
//	@PostMapping(value = "/link")
//	public void addLink(@RequestBody TaskLink taskLink) {
//		taskService.addLink(taskLink);
//	}
//
//	// 删除单个链接信息
//	@DeleteMapping(value = "/link/{id}")
//	public void deleteLink(
//			@PathVariable(value = "id") String linkId) {
//		taskService.deleteLink(linkId);
//	}


    // 获取任务的所有类型
    @GetMapping(value = "/allTaskType")
    public List<TaskType> getAllTaskType() {
        return taskService.getAllTaskType();
    }

    // 获取任务的所有优先级
    @GetMapping(value = "/allTaskPriority")
    public List<TaskPriority> getAllTaskPriority() {
        return taskService.getAllTaskPriority();
    }

    // 获取任务的所有状态
    @GetMapping(value = "/allTaskStatus")
    public List<TaskStatus> getAllTaskStatus() {
        return taskService.getAllTaskStatus();
    }

    // 获取能够派发任务的对象
    @GetMapping("/branch")
    public String getBranch(@CookieValue("access_token") String access_token) {
        return taskService.getReceiver(access_token);
    }

    // 获取能够派发任务的组对象
    @GetMapping("/branchGroup")
    public String getBranchGroup(@CookieValue("access_token") String access_token) {
        return taskService.getReceiverGroup(access_token);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseEnvelope<String>> handelError(Exception e) {
        e.printStackTrace();
        Integer code = null;
        if (e.getClass() == MissingServletRequestParameterException.class)
            code = 400;
        else
            code = 500;
        ResponseEnvelope<String> result = new ResponseEnvelope<String>(code, e.getMessage(), null);
        return new ResponseEntity<ResponseEnvelope<String>>(result, HttpStatus.valueOf(500));
    }
}
